//
//  WHDebugToolManager.m
//  WHDebugTool
//
//  Created by wuhao on 2018/7/17.
//  Copyright © 2018年 wuhao. All rights reserved.
//  https://github.com/remember17/WHDebugTool

#import "WHDebugToolManager.h"
#import "WHDebugFPSMonitor.h"
#import "WHDebugCpuMonitor.h"
#import "WHDebugMemoryMonitor.h"
#import "WHDebugConsoleLabel.h"

static inline BOOL debugIsIphoneX() {
    BOOL result = NO;
    if (UIDevice.currentDevice.userInterfaceIdiom != UIUserInterfaceIdiomPhone) {
        return result;
    }
    if (@available(iOS 11.0, *)) {
        UIWindow *mainWindow = [[[UIApplication sharedApplication] delegate] window];
        if (mainWindow.safeAreaInsets.bottom > 0.0) {
            result = YES;
        }
    }
    return result;
}
#define kDebugIsiPhoneX (debugIsIphoneX())
#define kDebugLabelWidth 70
#define kDebugLabelHeight 15
#define KDebugMargin 20

@interface WHDebugToolManager()

@property (nonatomic, strong) WHDebugConsoleLabel *memoryLabel;

@property (nonatomic, strong) WHDebugConsoleLabel *fpsLabel;

@property (nonatomic, strong) WHDebugConsoleLabel *cpuLabel;

@property (nonatomic, assign) BOOL isShowing;

@property(nonatomic, strong) UIWindow *debugWindow;

@end

@implementation WHDebugToolManager

static id _instance;
+ (instancetype)sharedInstance {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [[self alloc] init];
    });
    return _instance;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setDebugWindow];
        [self initMemoryLabel];
        [self initCpuLabel];
        [self initFpsLabel];
    }
    return self;
}

#pragma mark - Show with type
+ (void)showWith:(DebugToolType)type {
    WHDebugToolManager *manager = [WHDebugToolManager sharedInstance];
    manager.debugWindow.hidden = NO;
    if (type & DebugToolTypeMemory) {
        [manager showMemory];
    } else {
        [manager hideMemory];
    }
    
    if (type & DebugToolTypeCPU) {
        [manager showCPU];
    } else {
        [manager hideCPU];
    }
    
    if (type & DebugToolTypeFPS) {
        [manager showFPS];
    } else {
        [manager hideFPS];
    }
}

+ (void)hide
{
    WHDebugToolManager *manager = [WHDebugToolManager sharedInstance];
    manager.debugWindow.hidden = YES;
}

#pragma mark - Window

- (void)setDebugWindow {
    self.debugWindow = [[UIWindow alloc] init];
    self.debugWindow.backgroundColor = [UIColor clearColor];
    self.debugWindow.windowLevel = UIWindowLevelAlert;
    self.debugWindow.userInteractionEnabled = NO;
    PYViewController *vc = [PYViewController new];
    vc.view.backgroundColor = [UIColor clearColor];
    self.debugWindow.rootViewController = vc;
    self.debugWindow.hidden = NO;
}

#pragma mark - Memory

- (WHDebugConsoleLabel *)memoryLabel {
    if (!_memoryLabel) {
        _memoryLabel = [[WHDebugConsoleLabel alloc] initWithFrame:CGRectZero];
    }
    return _memoryLabel;
}

- (void)initMemoryLabel
{
    [self.debugWindow addSubview:self.memoryLabel];
    [self.memoryLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(-kDebugLabelWidth);
        make.top.mas_equalTo(APP_STATUSBAR_HEIGHT - 3);
        make.width.mas_equalTo(kDebugLabelWidth);
        make.height.mas_equalTo(kDebugLabelHeight);
    }];
}

- (void)showMemory {
    [[WHDebugMemoryMonitor sharedInstance] stopMonitoring];
    [[WHDebugMemoryMonitor sharedInstance] startMonitoring];
    [WHDebugMemoryMonitor sharedInstance].valueBlock = ^(float value) {
        [self.memoryLabel updateLabelWith:DebugToolLabelTypeMemory value:value];
    };
    [self.memoryLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(KDebugMargin);
    }];
    [UIView animateWithDuration:PY_Animation_Duration animations:^{
        [self.debugWindow layoutIfNeeded];
    }];
}

- (void)hideMemory
{
    [[WHDebugMemoryMonitor sharedInstance] stopMonitoring];
    [self.memoryLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(-kDebugLabelWidth);
    }];
    [UIView animateWithDuration:PY_Animation_Duration animations:^{
        [self.debugWindow layoutIfNeeded];
    }];
}

#pragma mark - CPU
- (WHDebugConsoleLabel *)cpuLabel {
    if (!_cpuLabel) {
        _cpuLabel = [[WHDebugConsoleLabel alloc] initWithFrame:CGRectZero];
    }
    return _cpuLabel;
}

- (void)initCpuLabel
{
    [self.debugWindow addSubview:self.cpuLabel];
    [self.cpuLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.top.mas_equalTo(-kDebugLabelHeight);
        make.width.mas_equalTo(kDebugLabelWidth);
        make.height.mas_equalTo(kDebugLabelHeight);
    }];
}

- (void)showCPU {
    [[WHDebugCpuMonitor sharedInstance] stopMonitoring];
    [[WHDebugCpuMonitor sharedInstance] startMonitoring];
    [WHDebugCpuMonitor sharedInstance].valueBlock = ^(float value) {
        [self.cpuLabel updateLabelWith:DebugToolLabelTypeCPU value:value];
    };
    [self.cpuLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(APP_STATUSBAR_HEIGHT - 3);
    }];
    [UIView animateWithDuration:PY_Animation_Duration animations:^{
        [self.debugWindow layoutIfNeeded];
    }];
}

- (void)hideCPU
{
    [[WHDebugCpuMonitor sharedInstance] stopMonitoring];
    [self.cpuLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(-kDebugLabelHeight);
    }];
    [UIView animateWithDuration:PY_Animation_Duration animations:^{
        [self.debugWindow layoutIfNeeded];
    }];
}

#pragma mark - FPS
- (WHDebugConsoleLabel *)fpsLabel {
    if (!_fpsLabel) {
        _fpsLabel = [[WHDebugConsoleLabel alloc] initWithFrame:CGRectZero];
    }
    return _fpsLabel;
}

- (void)initFpsLabel
{
    [self.debugWindow addSubview:self.fpsLabel];
    [self.fpsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(kDebugLabelWidth);
        make.top.mas_equalTo(APP_STATUSBAR_HEIGHT - 3);
        make.width.mas_equalTo(kDebugLabelWidth);
        make.height.mas_equalTo(kDebugLabelHeight);
    }];
}

- (void)showFPS {
    [[WHDebugFPSMonitor sharedInstance] stopMonitoring];
    [[WHDebugFPSMonitor sharedInstance] startMonitoring];
    [WHDebugFPSMonitor sharedInstance].valueBlock = ^(float value) {
        [self.fpsLabel updateLabelWith:DebugToolLabelTypeFPS value:value];
    };
    [self.fpsLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-KDebugMargin);
    }];
    [UIView animateWithDuration:PY_Animation_Duration animations:^{
        [self.debugWindow layoutIfNeeded];
    }];
}

- (void)hideFPS
{
    [[WHDebugFPSMonitor sharedInstance] stopMonitoring];
    [self.fpsLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(kDebugLabelWidth);
    }];
    [UIView animateWithDuration:PY_Animation_Duration animations:^{
        [self.debugWindow layoutIfNeeded];
    }];
}

@end
